<?php
include_once "conn.php";
$token=$_POST['token'];
$user=$_POST['user'];
$tel=$_POST['tel'];
$address=$_POST['address'];
$sql=$db->query("SELECT address_default FROM address WHERE 
                    address_default = 1 AND user_id = 
                    (SELECT user_id FROM user WHERE user_token = '$token')");
if ($sql->num_rows > 0) {
    $res=$db->query("INSERT INTO address (user_id,address_user,address_tel,address_info)
                VALUES ((SELECT user_id FROM user WHERE user_token = '$token'),
                '$user','$tel','$address')");
    if($res){
        echo json_encode(['msg'=>'success']);
    }else{
        echo json_encode(['msg'=>'error']);
    }
}else{
    $res=$db->query("INSERT INTO address (user_id,address_user,address_tel,address_info,address_default)
                VALUES ((SELECT user_id FROM user WHERE user_token = '$token'),
                '$user','$tel','$address',1)");
    if($res){
        echo json_encode(['msg'=>'success']);
    }else{
        echo json_encode(['msg'=>'error']);
    }
}
$db->close();
?>